package com.proton.carepatchtemp.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.adapter.ReportListAdapter;
import com.proton.carepatchtemp.base.BaseActivity;
import com.proton.carepatchtemp.constant.Codes;
import com.proton.carepatchtemp.constant.Extras;
import com.proton.carepatchtemp.database.table.Report;
import com.proton.carepatchtemp.listener.NoDoubleClickListener;
import com.proton.carepatchtemp.ui.SegmentView;
import com.proton.carepatchtemp.utils.NetUtils;
import com.proton.carepatchtemp.utils.TastyUtils;
import com.proton.carepatchtemp.utils.eventbus.MainEvent;
import com.proton.carepatchtemp.utils.net.ReportCenter;
import com.sdsmdg.tastytoast.TastyToast;

import java.util.ArrayList;

import cn.bingoogolapple.refreshlayout.BGANormalRefreshViewHolder;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

public class PrfReprotActivity extends BaseActivity implements BGARefreshLayout.BGARefreshLayoutDelegate {

    public boolean isCheckAll = false;
    ReportListAdapter mReportListAdapter;
    LinearLayoutManager mLinearLayoutManager;
    ArrayList<Report> data = new ArrayList<>();
    String profileId;
    int page = 2;
    private ImageButton mBtnBack;
    /**
     * --
     */
    private SegmentView mTvPrfName;
    private RecyclerView mRcvReportListPrf;
    private BGARefreshLayout mBgaRflReportPrf;
    private BGANormalRefreshViewHolder mBGANormalRefreshViewHolder;
    //显示全部or收藏标志位
    private int isAll = 1;
    /**
     * 编辑
     */
    private TextView mTvPrfReportEdit;
    /**
     * 取消
     */
    private TextView mTvCancelEdit;
    /**
     * 删除
     */
    private TextView mTvDeleteAllChecked;
    /**
     * 全选
     */
    private TextView mTvSelectAll;
    private LinearLayout mLlyMenu;
    private LinearLayout mLnlNullPrfReport;
    /**
     * 您还没有任何报告！赶紧去测量吧！
     */
    private TextView mTvNullPrfReport;
    /**
     * 重试
     */
    private TextView mTvNoNetRetry;
    private LinearLayout mLnlPrfReportNoNet;


    @Override
    public void dealEvent(MainEvent event) {
        switch (event.getId()) {
            case Codes.NET_RPT_DELETE:
                if (event.isSuccess()) {
                    TastyUtils.makeTextShort(R.string.delete_rpt_success, TastyToast.SUCCESS);
                    mReportListAdapter.mReports.removeAll(mReportListAdapter.mDelReportList);
                    mReportListAdapter.mDeleteList.clear();
                    mReportListAdapter.mDelReportList.clear();
                    mReportListAdapter.notifyDataSetChanged();
                    if (0 >= data.size()) {
                        mBgaRflReportPrf.beginRefreshing();
                    }
                } else {
                    mBgaRflReportPrf.beginRefreshing();
                    TastyUtils.makeTextShort(R.string.delete_rpt_failed, TastyToast.ERROR);
                }
                break;
            case Codes.REPORT_PRF_RPT_LIST:
                if (event.isSuccess()) {
                    if (event.getMsg().equalsIgnoreCase("1")) {
                        page = 2;
                        data.clear();
                        if (isAll == 2) {
                            for (Object r :
                                    event.getList()) {
                                Report report = (Report) r;
                                if (report.isCollect()) {
                                    data.add(report);
                                }
                            }
                        } else {
                            data.addAll(event.getList());
                        }

                        if (data.size() <= 0) {
                            mLnlNullPrfReport.setVisibility(View.VISIBLE);
                        } else {
                            mLnlNullPrfReport.setVisibility(View.INVISIBLE);
                        }
                        mReportListAdapter.notifyDataSetChanged();
                    } else {
                        page += 1;

                        if (isAll == 2) {
                            for (Object r :
                                    event.getList()) {
                                Report report = (Report) r;
                                if (report.isCollect()) {
                                    data.add(report);
                                }
                            }
                        } else {
                            data.addAll(event.getList());
                        }
                        mReportListAdapter.notifyDataSetChanged();
                    }
                } else {
                    TastyUtils.makeTextShort(R.string.get_rpt_failed, TastyToast.ERROR);
                }
                mBgaRflReportPrf.endLoadingMore();
                mBgaRflReportPrf.endRefreshing();
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_prf_reprot);

        if (TextUtils.isEmpty(getIntent().getExtras().getString(Extras.PROFILE))) {
            TastyUtils.makeTextShort(R.string.get_prf_failed, TastyToast.ERROR);
            return;
        }

        profileId = getIntent().getExtras().getString(Extras.PROFILE);
        initView();
        setListener();
        mReportListAdapter = new ReportListAdapter(this, data, getSupportFragmentManager());
        mLinearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRcvReportListPrf.setLayoutManager(mLinearLayoutManager);
        mRcvReportListPrf.setAdapter(mReportListAdapter);
        mBgaRflReportPrf.setDelegate(this);
        setBgaRefreshLayout();
        mBgaRflReportPrf.beginRefreshing();
    }

    private void setBgaRefreshLayout() {
        mBGANormalRefreshViewHolder = new BGANormalRefreshViewHolder(this, true);
        mBgaRflReportPrf.setIsShowLoadingMoreView(true);
        mBGANormalRefreshViewHolder.setLoadingMoreText(getString(R.string.loading));
        mBGANormalRefreshViewHolder.setPullDownRefreshText(getString(R.string.string_pull_2_refresh));
        mBGANormalRefreshViewHolder.setRefreshingText(getString(R.string.loading));
        mBGANormalRefreshViewHolder.setReleaseRefreshText(getString(R.string.string_release_text));
        mBgaRflReportPrf.setRefreshViewHolder(mBGANormalRefreshViewHolder);
    }

    @Override
    protected void setListener() {
        mBtnBack.setOnClickListener(v -> {
            finish();
        });
        mTvPrfName.setOnSegmentViewClickListener((v, position) -> {
            switch (position) {
                case 0:
                    isAll = 1;
                    mTvNullPrfReport.setText(R.string.string_no_report_go_to_measure);
                    mBgaRflReportPrf.beginRefreshing();
                    mReportListAdapter.mDeleteList.clear();
                    mReportListAdapter.mDelReportList.clear();
                    isCheckAll = false;
                    break;
                case 1:
                    isAll = 2;
                    mTvNullPrfReport.setText(R.string.string_no_collect_report);
                    mBgaRflReportPrf.beginRefreshing();
                    mReportListAdapter.mDeleteList.clear();
                    mReportListAdapter.mDelReportList.clear();
                    isCheckAll = false;
                    break;
                default:
                    break;
            }
        });

        NoDoubleClickListener l = new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                mTvCancelEdit.setVisibility(View.GONE);
                mTvPrfReportEdit.setVisibility(View.VISIBLE);
                mReportListAdapter.isBulkDeletion = false;
                isCheckAll = false;
                for (Report r :
                        mReportListAdapter.mDelReportList) {
                    r.setChecked(false);
                }
                mReportListAdapter.mDeleteList.clear();
                mReportListAdapter.mDelReportList.clear();
                mLlyMenu.animate()
                        .alpha(0f)
                        .translationYBy(mLlyMenu.getHeight())
                        .setDuration(100).start();
                mRcvReportListPrf.setPadding(0, 0, 0, 0);
                mReportListAdapter.notifyDataSetChanged();
            }
        };
        mTvCancelEdit.setOnClickListener(l);

        NoDoubleClickListener l1 = new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                mTvPrfReportEdit.setVisibility(View.GONE);
                mTvCancelEdit.setVisibility(View.VISIBLE);
                mLlyMenu.animate()
                        .alpha(1.0f)
                        .translationYBy(-mLlyMenu.getHeight())
                        .setDuration(100).start();
                mRcvReportListPrf.setPadding(0, 0, 0, mLlyMenu.getHeight());
                mReportListAdapter.isBulkDeletion = true;
                mReportListAdapter.notifyDataSetChanged();
            }
        };
        mTvPrfReportEdit.setOnClickListener(l1);

        mTvSelectAll.setOnClickListener(v -> {
            if (isCheckAll) {
                isCheckAll = false;
                for (Report r :
                        data) {
                    r.setChecked(false);
                }
                mReportListAdapter.mDelReportList.clear();
                mReportListAdapter.mDeleteList.clear();
            } else {
                isCheckAll = true;
                mReportListAdapter.mDelReportList.clear();
                mReportListAdapter.mDelReportList.addAll(data);
                for (Report r :
                        data) {
                    mReportListAdapter.mDeleteList.add(r.getId() + "");
                    r.setChecked(true);
                }
            }
            mReportListAdapter.notifyDataSetChanged();
        });

        mTvDeleteAllChecked.setOnClickListener(v -> {
            if (mReportListAdapter.mDeleteList.size() > 0) {
                ReportCenter.deleteSome(mReportListAdapter.mDeleteList.toString().replace("[", "").replace("]", "").replaceAll(" ", ""));
            }
        });

        mTvNullPrfReport.setOnClickListener(v -> {
            mBgaRflReportPrf.beginRefreshing();
        });
    }

    private void initView() {
        mBtnBack = findViewById(R.id.btn_back);
        mTvPrfName = findViewById(R.id.tv_prf_name);
        mRcvReportListPrf = findViewById(R.id.rcv_report_list_prf);
        mBgaRflReportPrf = findViewById(R.id.bga_rfl_report_prf);
        mTvPrfName.setSegmentText("   " + getString(R.string.string_all) + "   ", 0);
        mTvPrfName.setSegmentText(getString(R.string.string_marked), 1);
        mTvPrfReportEdit = findViewById(R.id.tv_prf_report_edit);
        mTvCancelEdit = findViewById(R.id.tv_cancel_edit);
        mTvDeleteAllChecked = findViewById(R.id.tv_delete_all_checked);
        mTvSelectAll = findViewById(R.id.tv_select_all);
        mLlyMenu = findViewById(R.id.lly_menu);
        mLnlNullPrfReport = findViewById(R.id.lnl_null_prf_report);
        mTvNullPrfReport = findViewById(R.id.tv_null_prf_report);
        mTvNoNetRetry = findViewById(R.id.tv_no_net_retry);
        mLnlPrfReportNoNet = findViewById(R.id.lnl_prf_report_no_net);
    }

    @Override
    public void onEventMainThread(MainEvent event) {
        dealOnEvent(event);
    }


    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        if (NetUtils.noNet()) {
//            data.clear();
//            mReportListAdapter.notifyDataSetChanged();
            mRcvReportListPrf.setVisibility(View.GONE);
            mLnlNullPrfReport.setVisibility(View.GONE);
            mLnlPrfReportNoNet.setVisibility(View.VISIBLE);
            return;
        } else {
            mRcvReportListPrf.setVisibility(View.VISIBLE);
            mLnlPrfReportNoNet.setVisibility(View.GONE);
            mLnlNullPrfReport.setVisibility(View.GONE);
        }
        if (isAll == 2) {
            ReportCenter.getPrfCollectRPTList(profileId, 1, ReportCenter.PS_RPT);
        } else {
            ReportCenter.getPrfReportList(profileId, 1, ReportCenter.PS_RPT);
        }
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {

        if (NetUtils.noNet()) {
//            data.clear();
//            mReportListAdapter.notifyDataSetChanged();
            mRcvReportListPrf.setVisibility(View.GONE);
            mLnlNullPrfReport.setVisibility(View.GONE);
            mLnlPrfReportNoNet.setVisibility(View.VISIBLE);
            return false;
        } else {
            mRcvReportListPrf.setVisibility(View.VISIBLE);
            mLnlPrfReportNoNet.setVisibility(View.GONE);
            mLnlNullPrfReport.setVisibility(View.GONE);
        }

        if (isAll == 2) {
            ReportCenter.getPrfCollectRPTList(profileId, page, ReportCenter.PS_RPT);
        } else {
            ReportCenter.getPrfReportList(profileId, page, ReportCenter.PS_RPT);
        }

        return false;
    }
}
